#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
using namespace std;
int main() {
    std::ifstream file("/home/dmf/test/ttzzbb/final_h3_point_22km.csv"); // 打开文件
    std::ifstream oldfile("/home/dmf/test/ttzzbb/output.csv"); // 打开文件
    ofstream outcsvFile("/home/dmf/test/ttzzbb/output_22km.csv"); // 创建新的CSV文件
    if (!file.is_open() || !outcsvFile.is_open() || !oldfile.is_open()) {
        std::cerr << "Unable to open file." << std::endl;
        return 1;
    }
    std::string line;
    std::string newColumn = ",NewData"; // 要添加的新列数据

    map<string,string> id_num;
    while (std::getline(oldfile, line)) {
        std::istringstream s(line);
        std::string cell;
        std::vector<std::string> row;
        // 按逗号分割每行
        while (std::getline(s, cell, ',')) {
            row.push_back(cell);
        }

        // 将第一列作为键，其余列作为值
        if (!row.empty()) {
            id_num[row[0]]=row[3];
        }
        
    }
    for(auto& pair:id_num){
        cout<<pair.first<<", "<<pair.second<<endl;
    }
    oldfile.close();

    while (std::getline(file, line)) {
        std::istringstream iss(line);
        std::string cell;
        std::vector<std::string> row1;
        // 按逗号分割每行
        while (std::getline(iss, cell, ',')) {
            row1.push_back(cell);
        }
        string new_id = row1[0] + '_' + row1[1];
        outcsvFile << new_id << ",";
        outcsvFile << row1[2] << ",";
        outcsvFile << row1[3]<< ",";
        outcsvFile << id_num[row1[0]]; // 写入新文件
        outcsvFile << endl;
    }

    file.close();
    outcsvFile.close();

    std::cout << "CSV processing completed." << std::endl;

    return 0;
}